package cn.yunhe.insurance.controller;


import cn.yunhe.insurance.entity.Case;
import cn.yunhe.insurance.entity.PayBack;
import cn.yunhe.insurance.service.impl.PayBackServiceImpl;
import cn.yunhe.insurance.util.HideDataUtil;
import cn.yunhe.insurance.util.Result;
import com.github.pagehelper.PageInfo;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.json.MappingJackson2JsonView;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/payBack")
public class PayBackController {
    @Resource
    private PayBackServiceImpl payBackService;

    @RequestMapping("/to_payBack")
    public ModelAndView test() {
        return new ModelAndView("payBack_list");
    }

    @RequestMapping("/to_payBackRecord")
    public ModelAndView toPayBackRecord() {
        return new ModelAndView("payBackRecord_list");
    }

    @RequestMapping("/showJson")
    public ModelAndView showJsonInfo() {
        ModelAndView mv = new ModelAndView();
        mv.setView(new MappingJackson2JsonView());
        return mv;
    }

    @RequestMapping("/to_addPayBack")
    public ModelAndView toAddPayRecord(String caseId) {
        Case cases = payBackService.queryCaseByCaseId(caseId);
        ModelAndView mv = new ModelAndView();
        mv.addObject("cases", cases);
        mv.setViewName("payBack_add");
        return mv;
    }

    @RequestMapping("/addPayBack")
    public String addPayRecord(PayBack payBack) {
        boolean result = payBackService.addPayRecord(payBack);
        if (result) {
            return "success";
        } else {
            return "failure";
        }
    }

    @RequestMapping("/deletePayBack")
    public Result deletePayRecordById(PayBack payBack) {
        boolean flag = payBackService.deletePayRecordById(payBack);
        Result result = new Result();
        if (flag) {
            result.setCode(2001);
            result.setMessage("删除成功");
            return result;
        } else {
            result.setCode(2002);
            result.setMessage("删除失败");
            return result;
        }
    }

    @RequestMapping("/batchDelete")
    public Result batchDeletePayRecord(String[] uuidList) {
        List<String> idList = Arrays.asList(uuidList);
        boolean flag = payBackService.batchDeletePayRecord(idList);
        Result result = new Result();
        if (flag) {
            result.setCode(4001);
            result.setMessage("批量删除成功");
            return result;
        } else {
            result.setCode(4002);
            result.setMessage("批量删除失败");
            return result;
        }
    }

    @RequestMapping("/updatePayStatus")
    public Result updatePayBackStatus(PayBack payBack) {
        boolean flag = payBackService.updatePayBackStatus(payBack);
        Result result = new Result();
        if (flag) {
            result.setCode(3001);
            result.setMessage("修改成功");
            return result;
        } else {
            result.setCode(3002);
            result.setMessage("修改失败");
            return result;
        }
    }

    @RequestMapping("/queryAllPayList")
    public List<PayBack> queryAllPayList() {
        List<PayBack> payBacks = payBackService.queryAllPayList();
        return payBacks;
    }

    @RequestMapping("/queryPayListByLike")
    public PageInfo<PayBack> queryPayBackListByLike(int pageNum, int pageSize, String keyword) {
        Map<String, Object> map = new HashMap<>();
        map.put("pageNum", pageNum);
        map.put("pageSize", pageSize);
        map.put("keyword", keyword);
        PageInfo<PayBack> pageInfo = payBackService.queryPayBackListByLike(map);
        return pageInfo;
    }

    @RequestMapping("/queryByPayStatus")
    public PageInfo<PayBack> queryPayRecordByStatus(int pageNum, int pageSize, int payStatus, String keyword) {
        Map<String, Object> map = new HashMap<>();
        map.put("pageNum", pageNum);
        map.put("pageSize", pageSize);
        map.put("payStatus", payStatus);
        map.put("keyword", keyword);
        PageInfo<PayBack> pageInfo = payBackService.queryPayRecordByStatus(map);
        return pageInfo;
    }

    @RequestMapping("/queryUnPayRecord")
    public PageInfo<PayBack> queryUnPayRecord(int pageNum, int pageSize, String keyword) {
        Map<String, Object> map = new HashMap<>();
        map.put("pageNum", pageNum);
        map.put("pageSize", pageSize);
        map.put("keyword", keyword);
        PageInfo<PayBack> pageInfo = payBackService.queryUnPayRecord(map);
        return pageInfo;
    }

    @RequestMapping("/queryIsPayRecord")
    public PageInfo<PayBack> queryIsPayRecord(int pageNum, int pageSize, String keyword) {
        Map<String, Object> maps = new HashMap<>();
        maps.put("pageNum", pageNum);
        maps.put("pageSize", pageSize);
        maps.put("keyword", keyword);
        PageInfo<PayBack> pageInfo = payBackService.queryIsPayRecord(maps);
        for (PayBack payBack:pageInfo.getList()) {
            payBack.setPayName(HideDataUtil.hideUserName(payBack.getPayName()));
            payBack.setCreditCard(HideDataUtil.hideCardNo(payBack.getCreditCard()));
            payBack.setUserCardId(HideDataUtil.hideIdCard(payBack.getUserCardId()));
        }
        return pageInfo;
    }

    @RequestMapping("/queryCaseByCaseId")
    public Case queryCaseByCaseId(String caseId) {
        Case cases = payBackService.queryCaseByCaseId(caseId);
        return cases;
    }

    @RequestMapping("/queryUnPayCase")
    public PageInfo<Case> queryCaseListUnPay(int pageNum, int pageSize, String keyword) {
        Map<String, Object> maps = new HashMap<>();
        maps.put("pageNum", pageNum);
        maps.put("pageSize", pageSize);
        maps.put("keyword", keyword);
        PageInfo<Case> pageInfo = payBackService.queryCaseListUnPay(maps);
        return pageInfo;
    }

    @RequestMapping("/updateCaseState")
    public Result updateCaseState(Case cases) {
        boolean flag = payBackService.updateCaseState(cases);
        Result result = new Result();
        if (flag) {
            result.setCode(5001);
            result.setMessage("状态修改成功");
            return result;
        } else {
            result.setCode(5002);
            result.setMessage("状态修改失败");
            return result;
        }
    }

    public PayBackServiceImpl getPayBackService() {
        return payBackService;
    }

    public void setPayBackService(PayBackServiceImpl payBackService) {
        this.payBackService = payBackService;
    }
}
